import Vue from "vue";
import Router from "vue-router";
import HelloWorld from "../components/HelloWorld";
import Login from "../page/Login";

import TanHome from "../page/admin/TanHome";
import TanHeader from "../components/TanHeader.vue";
import TanPage from "../page/tan/TanPage";
import TanData from "../page/tan/TanData";
import TanBuilding from "../page/tan/TanBuilding";
import TanSchool from "../page/tan/TanSchool";
import ManageEnergy from "../page/admin/ManageEnergy";
import ManageCarbon from "../page/admin/ManageCarbon";
import AdminHeader from "../components/AdminHeader";
import adminPage from "../page/admin/adminPage";
import { Message } from "element-ui";
Vue.prototype.$message = Message;
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect:"/Tan/TanPage"
    },
    {
      path: "/Login",
      redirect:"/Tan/TanLogin"
    },
    {
      path: "/Tan",
      name: "Tan",
      component: TanHeader,
      redirect: "/Tan/TanPage",
      children: [
        {
          path: "TanPage",
          name: "TanPage",
          component: TanPage
        },
        {
          path: "TanData",
          name: "TanData",
          component: TanData
        },
        {
          path: "TanBuilding",
          name: "TanBuilding",
          component: TanBuilding
        },
        {
          path: "TanSchool",
          name: "TanSchool",
          component: TanSchool
        },
        {
          path: "TanLogin",
          name: "TanLogin",
          component: Login
        },
        {
          path: "ManageEnergy",
          name: "ManageEnergy",
          component: ManageEnergy,
          meta: {
            authRequired: true, //开启路由权限验证
            requireRole: "admin"//身份须为'admin'
          }
        },
        {
          path: "ManageCarbon",
          name: "ManageCarbon",
          component: ManageCarbon,
          meta: {
            authRequired: true, //开启路由权限验证
            requireRole: "admin"//身份须为'admin'
          }
        }
      ]
    }
  ]
});

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
//
// 路由前置守卫：权限校验
router.beforeEach(async (to, from, next) => {
  // console.log("已拦截，localStorage.getItem('auth')",localStorage.getItem("auth"));
  const { authRequired, requireRole } = to.meta;
  // console.log("to:", to);
  if (authRequired === true) {
    const auth_role = localStorage.getItem("auth");
    console.log("to.path:",to.path)
    if (auth_role === requireRole) {
      next();
    } else {
      Vue.prototype.$message("请先完成登录！");
      next({
        name: 'TanLogin',
        params:{
          curRoute:to.path
        }
      });
    }
  //无效路由，直接跳回首页
  } else if(to.matched.length === 0) {
    next({
      path: '/Tan/TanPage',
    });
  }else{
    next();
  }
});

router.afterEach((to, from) => {
  // 跳转之后滚动条回到顶部
  window.scrollTo(0, 0);
});

export default router;
